package com.hb.software.controller.worker;

import com.hb.software.controller.BaseController;
import com.hb.software.domain.Result;
import com.hb.software.entity.worker.Worker;
import com.hb.software.worker.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("worker")
public class WorkerController extends BaseController<Worker, Long, WorkerService> {

    private static final Logger logger = LogManager.getLogger(WorkerController.class.getName());

    private WorkerService workerService;

    @Autowired
    public void setWorkerService(WorkerService workerService) {
        this.workerService = workerService;
        this.manager = this.workerService;
    }

    @PostMapping("/findByName")
    public Result findByName(@RequestBody String name) throws UnsupportedEncodingException {
        //处理中文乱码问题
        name = URLDecoder.decode(name, "UTF-8");
        name = name.substring(0, name.length() - 1);
        List list = workerService.findByName(name);
        return Result.success(list);
    }

    @PostMapping("/findByDepartment")
    public Result findByDepartment(@RequestBody String did) {
        Long id = Long.valueOf(did.substring(0, did.length() - 1));
        List list = workerService.findByDepartment(id);
        return Result.success(list);
    }

    @PostMapping("/findLastId")
    public Result findLastId() {
        long id = workerService.findLastId();
        return Result.success(id);
    }

    @PostMapping("/exportExcel")
    public Result exportExcel(@RequestBody String path) throws UnsupportedEncodingException {
        //处理中文乱码问题
        path = URLDecoder.decode(path, "UTF-8");
        path = path.substring(0, path.length() - 1);
        List list = workerService.findAll();

        String str = workerService.createExcel(list, path);
        if (str.equals("success"))
            return Result.success();
        else return Result.error("路径错误");
    }

    @PostMapping("/findByPhone")
    public Result findByPhone(@RequestBody String phone) {
        List list = new ArrayList();
        try {
            list = workerService.findByPhone(phone.substring(0, phone.length() - 1));
        } catch (Exception e) {

        }
        return Result.success(list);
    }
}



